package com.yf.exam.modules.user.repo.controller;

import com.yf.exam.core.api.ApiRest;
import com.yf.exam.core.api.controller.BaseController;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.modules.paper.service.PaperService;
import com.yf.exam.modules.repo.service.RepoService;
import com.yf.exam.modules.user.book.service.UserBookService;
import com.yf.exam.modules.user.repo.dto.request.UserRepoReqDTO;
import com.yf.exam.modules.user.repo.service.UserRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户题库控制器
 * 用于处理用户与题库相关的训练、考试等操作
 */
@Api(tags = {"用户题库"})
@RestController
@RequestMapping("/exam/api/user/repo")
public class UserRepoController extends BaseController {

    @Autowired
    private UserRepoService userRepoService;

    @Autowired
    private PaperService paperService;
    
    @Autowired
    private RepoService repoService;
    
    /**
     * 开始训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "开始训练")
    @RequestMapping(value = "/start", method = { RequestMethod.POST})
    public ApiRest<BaseIdReqDTO> startTrain(@RequestBody UserRepoReqDTO reqDTO) {
        try {
            // 如果是考试模式(1)，则创建模拟试卷
            if (reqDTO.getMode() != null && reqDTO.getMode() == 1) {
                // 创建模拟试卷
                String paperId = paperService.createSimulationPaper(reqDTO.getUserId(), reqDTO.getRepoId());
                BaseIdReqDTO respDTO = new BaseIdReqDTO();
                respDTO.setId(paperId);
                return super.success(respDTO);
            }
            
            BaseIdReqDTO respDTO = userRepoService.startTrain(reqDTO);
            return super.success(respDTO);
        } catch (Exception e) {
            return super.failure(500, "开始训练失败: " + e.getMessage(), null);
        }
    }
    
    @Autowired
    private UserBookService userBookService;
    
    /**
     * 填充答案结果
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "填充答案结果")
    @RequestMapping(value = "/fill", method = { RequestMethod.POST})
    public ApiRest fillResult(@RequestBody UserRepoReqDTO reqDTO) {
        try {
            // 检查答案是否正确，clear为true表示正确，false或null表示错误
            boolean isCorrect = reqDTO.getClear() != null && reqDTO.getClear();
            
            // 如果答案错误，将题目加入错题本
            if (!isCorrect) {
                // repoId 作为 examId 使用
                userBookService.addBookWithSource(reqDTO.getRepoId(), reqDTO.getSequence(), 1);
            }
            
            return super.success();
        } catch (Exception e) {
            return super.failure(500, "保存答案结果失败: " + e.getMessage(), null);
        }
    }
    
    /**
     * 检查是否存在训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "检查是否存在训练")
    @RequestMapping(value = "/check", method = { RequestMethod.POST})
    public ApiRest<Boolean> hasTrain(@RequestBody UserRepoReqDTO reqDTO) {
        // TODO: 实现检查是否存在训练逻辑
        return super.success(false);
    }
    
    /**
     * 获取答题卡
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "获取答题卡")
    @RequestMapping(value = "/card", method = { RequestMethod.POST})
    public ApiRest card(@RequestBody UserRepoReqDTO reqDTO) {
        // TODO: 实现获取答题卡逻辑
        return super.success();
    }
    
    /**
     * 获取下一题
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "获取下一题")
    @RequestMapping(value = "/next", method = { RequestMethod.POST})
    public ApiRest<BaseIdReqDTO> nextQu(@RequestBody UserRepoReqDTO reqDTO) {
        try {
            BaseIdReqDTO respDTO = userRepoService.nextQu(reqDTO);
            return super.success(respDTO);
        } catch (Exception e) {
            return super.failure(500, "获取下一题失败: " + e.getMessage(), null);
        }
    }
}